package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.entity.TGoodVarietyTbl;
import com.dom.domweb.dao.mapper.TGoodVarietyTblMapper;
import com.dom.domweb.dao.service.ITGoodVarietyTblService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * B_商品品种代码表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TGoodVarietyTblServiceImpl extends ServiceImpl<TGoodVarietyTblMapper, TGoodVarietyTbl> implements ITGoodVarietyTblService {

    @Override
    public B119A getVarietyInfoList(B119R request) {
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper();
//        wrapper.notIn("stat", PubStatEnum.STAT_STOP.getCode());
        Page<TGoodVarietyTbl> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());
        page = this.selectPage(page,wrapper);
        B119A result = new B119A();
        result.setTotalNum((int)page.getTotal());
        List<VarietyInfo> list = infoToTbl(page.getRecords());
        result.setVarietyData(list);
        return result;
    }

    @Override
    public Map<String,Object> getAllVarietyInMap(){
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper();
        List<TGoodVarietyTbl> list = this.baseMapper.selectList(wrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        for (TGoodVarietyTbl tbl: list) {
            map.put(tbl.getVarietyCode().toString(),tbl.getVarietyName());
        }
        return map;
    }

    @Override
    public RspModel insertVarietyInfo(B110R request) {
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper<TGoodVarietyTbl>();
        wrapper.eq("variety_code",request.getVarietyCode());
        List<TGoodVarietyTbl> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0){
            return new RspModel("RSP000030","商品品种代码已存在！");
        }
        TGoodVarietyTbl tblInfo = new TGoodVarietyTbl();
        tblInfo.setFatherId(request.getFatherVarietyId());
        tblInfo.setStat(PubStatEnum.STAT_UNACTIVATED.getCode());
        tblInfo.setMemCode(0);
        tblInfo.setVarietyCode(StringUtils.isEmpty(request.getVarietyCode()) ? null : Integer.valueOf(request.getVarietyCode()));
        tblInfo.setVarietyName(request.getVarietyName());
        tblInfo.setVersion(0);
        int count = this.baseMapper.insert(tblInfo);
        RspModel model = new RspModel("RSP000000","商品信息新增成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel editVarietyInfo(B110R request) {
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper<TGoodVarietyTbl>();
        wrapper.eq("id",request.getId());
        TGoodVarietyTbl tbl = this.baseMapper.selectById(request.getId());
        if (tbl.getStat() != PubStatEnum.STAT_UNACTIVATED.getCode()
             &&  !request.getVarietyCode().equals(Integer.toString(tbl.getVarietyCode())) ){
            return new RspModel(ConstantError.NOT_CODE_ERROR);
        }
        TGoodVarietyTbl tblInfo = new TGoodVarietyTbl();
        tblInfo.setFatherId(request.getFatherVarietyId());
        tblInfo.setVarietyCode(StringUtils.isEmpty(request.getVarietyCode()) ? null : Integer.valueOf(request.getVarietyCode()));
        tblInfo.setVarietyName(request.getVarietyName());
        tblInfo.setStat(StringUtils.isEmpty(request.getStat()) ? PubStatEnum.STAT_UNACTIVATED.getCode() : Integer.valueOf(request.getStat()));
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品信息修改成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel varietyInfoDel(String id) {
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper<TGoodVarietyTbl>();
        wrapper.eq("id",id);
        TGoodVarietyTbl tblInfo = new TGoodVarietyTbl();
        tblInfo.setStat(PubStatEnum.STAT_STOP.getCode());
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品信息删除成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    private Map<String,String> getVarietyInfoToMap(){
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper<>();
        List<TGoodVarietyTbl> list = this.baseMapper.selectList(wrapper);
        Map<String,String> map = new HashMap<String,String>();
        for (TGoodVarietyTbl tbl: list) {
            map.put(tbl.getVarietyCode().toString(),tbl.getVarietyName());
        }
        return map;
    }

    private List<VarietyInfo> infoToTbl(List<TGoodVarietyTbl> list){
        List<VarietyInfo> result = new ArrayList<>();
        Map<String,String> map = getVarietyInfoToMap();
        for (TGoodVarietyTbl tblInfo:  list) {
            VarietyInfo info = new VarietyInfo();
            info.setId(tblInfo.getId().toString());
            info.setVarietyCode(tblInfo.getVarietyCode());
            info.setVarietyName(tblInfo.getVarietyName());
            info.setFatherVarietyCode(tblInfo.getFatherId());
            info.setFatherVarietyName(tblInfo.getFatherId() == null ? null : map.get(tblInfo.getFatherId().toString()));
            info.setStatName(PubStatEnum.getName(tblInfo.getStat()));
            result.add(info);
        }
        return result;
    }

    @Override
    public B119A getAllVarietyInfo() {
        B119A b = new B119A();
        Wrapper<TGoodVarietyTbl> wrapper = new EntityWrapper();
        List<TGoodVarietyTbl> list = this.baseMapper.selectList(wrapper);
        List<VarietyInfo> result = infoToTbl(list);
        b.setVarietyData(result);
        return b;
    }

    @Override
    public B113A getVarietyInfoByCode(String id) {
        TGoodVarietyTbl tbl =  this.baseMapper.selectById(id);
        B113A result = new B113A();
        result.setFatherVarietyId(tbl.getFatherId());
        result.setId(tbl.getId().toString());
        result.setVarietyCode(tbl.getVarietyCode().toString());
        result.setVarietyName(tbl.getVarietyName());
        result.setStat(tbl.getStat().toString());
        return result;
    }
}
